package web;

import domain.cms.CmsHomePage;
import domain.cms.CmsPage;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkIconPanel;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountIndexedParam;
import service.facade.user.CmsUserService;
import web.cms.CmsMainPage;
import web.cms.HomePage;
import web.eshop.panels.CategoryTreePanel;
import web.panels.TopMenuPanel;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import java.util.List;

@MountPath(path = "pagemap")
@SuppressWarnings("serial")
@MountIndexedParam
public final class MapPage extends BasePage {

    @SpringBean
    private CmsUserService cmsUserService;
    CategoryTreePanel categoryTreePanel;

    @Override
    protected Component newLeftPanel(String id) {
        return new EmptyPanel(id);
    }

    @Override
    protected Component newRightPanel(String id) {
        return new EmptyPanel(id);
    }

    @Override
    protected Component newTopPanel(String id) {
        return new TopMenuPanel(id);
    }

    public MapPage() {
        super();

        add(new CmsBookmarkablePageLinkTree("tree", createTreeModel().getObject()));

//        Tree tree = new Tree("tree", createTreeModel()) {
//
//            @Override
//            protected void onNodeLinkClicked(AjaxRequestTarget target, TreeNode node) {
//                super.onNodeLinkClicked(target, node);
////                CmsPage mp = (CmsPage) ((DefaultMutableTreeNode) node).getUserObject();
////        replacePanel(mp.getId());
//            }
//        };
//        // disable ajax links in this example
//        tree.setLinkType(LinkType.REGULAR);
//        tree.getTreeState().expandAll();
//        tree.setRootLess(true);
//        add(tree);

    }


    private LoadableDetachableModel<TreeModel> createTreeModel() {
        LoadableDetachableModel<TreeModel> treeModel = new LoadableDetachableModel<TreeModel>() {

            @Override
            protected TreeModel load() {
                final DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(cmsUserService.getCmsHomePage());
                add(rootNode, cmsUserService.getSections());
                return new DefaultTreeModel(rootNode);
            }
        };
        return treeModel;
    }

    private void add(DefaultMutableTreeNode parent, List<CmsPage> sub) {
        for (CmsPage mp : sub) {
            List<CmsPage> childs = mp.getChilds();
            if (childs.size() != 0) {
                DefaultMutableTreeNode subParent = new DefaultMutableTreeNode(mp);
                parent.add(subParent);
                add(subParent, childs);
            } else {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(mp);
                parent.add(child);
            }
        }
    }

    @Override
    public IModel getPageTitle() {
        return new Model("MapPage");
    }

    @Override
    public IModel getDescription() {
        return new Model("MapPage");
    }

    @Override
    public IModel getKeywords() {
        return new Model("MapPage");
    }

    class CmsBookmarkablePageLinkTree extends LinkTree {
        public CmsBookmarkablePageLinkTree(String id, TreeModel model) {
            super(id, model);
            setLinkType(LinkType.AJAX_FALLBACK);
            getTreeState().expandAll();
            getTreeState().setAllowSelectMultiple(false);

        }

        @Override
        protected Component newNodeComponent(String id, IModel model) {
            return new LinkIconPanel(id, model, CmsBookmarkablePageLinkTree.this) {
                private static final long serialVersionUID = 1L;

                @Override
                protected void addComponents(final IModel model, final BaseTree tree) {
                    final IModel objModel = new LoadableDetachableModel() {

                        @Override
                        protected Object load() {
                            return ((DefaultMutableTreeNode) model.getObject()).getUserObject();
                        }
                    };
                    ILinkCallback callback = new ILinkCallback() {
                        private static final long serialVersionUID = 1L;

                        public void onClick(AjaxRequestTarget target) {
                            if (target == null) {
                            } else {
                                PageParameters params = new PageParameters();
                                if (objModel.getObject() instanceof CmsHomePage) {
                                    target.appendJavascript("window.location=" + CmsBookmarkablePageLinkTree.this.urlFor(
                                            HomePage.class, params));
                                } else if (objModel.getObject() instanceof CmsPage) {
                                    params.add("0", ((CmsPage) objModel.getObject()).getUrl());

                                    target.appendJavascript("window.location=" + CmsBookmarkablePageLinkTree.this.urlFor(
                                            CmsMainPage.class, params));
                                }
                            }
                        }
                    };

                    MarkupContainer iconContainer = tree.newLink("iconLink", callback);
                    iconContainer.add(newImageComponent("icon", tree, model));
                    add(iconContainer);

                    PageParameters params = new PageParameters();
                    if (objModel.getObject() instanceof CmsHomePage) {
                        Link contentLink = new BookmarkablePageLink("contentLink", HomePage.class, params);
                        contentLink.add(new Label("content", objModel.getObject().toString())
                        );
                        add(contentLink);
                    } else if (objModel.getObject() instanceof CmsPage) {
                        params.add("0", ((CmsPage) objModel.getObject()).getUrl());
                        Link contentLink = new BookmarkablePageLink("contentLink", CmsMainPage.class, params);
                        add(contentLink);
                        contentLink.add(new Label("content", objModel.getObject().toString()));
                    }
                }
            };
        }
    }

}
